package com.situ.tos2024.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.tos2024.dao.CategoryDAO;
import com.situ.tos2024.model.Category;
import com.situ.tos2024.service.CategoryService;
import com.situ.tos2024.util.PaginateInfo;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能：
 *
 * @author 千堆雪
 * @version 1.0.0
 * @since 2023/12/26
 * <p>
 * created by 千堆雪 on 2023/12/26, last modified by 千堆雪 on 2023/12/26
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    private final CategoryDAO categoryDAO;

    public CategoryServiceImpl(CategoryDAO categoryDAO) {
        this.categoryDAO = categoryDAO;
    }

    @Override
    public List<Category> findAll(PaginateInfo pi, Category search) {
        //自动分页插件的操作
        //1.启动分页
        try (Page<?> p = PageHelper.startPage(pi.getPageNo(), pi.getPageSize());) {
            //2.正常进行查询
            List<Category> categories = categoryDAO.findAll(search);

            //3.获取分页数据
            PageInfo<Category> pif = new PageInfo<>(categories);
            //pif.calcByNavigatePages(10);//根据导航页共10页进行计算
            pi.setPageInfo(pif);//将分页对象缓存

            return categories;
        }
    }
}
